// 记忆函数
const _ = require('lodash');

function getArea (r) {
  console.log(r);
  return Math.PI * r * r;
}

// let getAreaWithMomory = _.memoize(getArea);
// console.log(getAreaWithMomory(4));
// console.log(getAreaWithMomory(4));
// console.log(getAreaWithMomory(4));


// 模拟实现 memoize
function memoize (f) {
  let cache = {};
  return function () {
    let key = JSON.stringify(arguments);

    cache[key] = cache[key] || f.apply(f, arguments);

    return cache[key];
  }
}

let getAreaWithMomory = memoize(getArea);
console.log(getAreaWithMomory(4));
console.log(getAreaWithMomory(4));
console.log(getAreaWithMomory(4));